package com.zq.myapp;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.toolbox.StringRequest;

import java.util.Map;

public class VolleryRequest {

    public static StringRequest stringRequest;
    public static Context context;

    /**
     * 自定义的get请求------------------volley的简单的二次回调封装
     * @param mContext
     * @param url
     * @param tag
     * @param vif
     */
    public static void RequestGet(Context mContext,String url,String tag,VolleyInterface vif){
        MyApplication.getHttpQueues().cancelAll(tag);//先清空
        //利用vif接口调用成功监听和失败监听
        stringRequest = new StringRequest(Request.Method.GET,url,vif.loadingListener(),
                    vif.errorListener());
        stringRequest.setTag(tag);//再重新设置标签
        MyApplication.getHttpQueues().add(stringRequest);//再将请求添加到队列里
        MyApplication.getHttpQueues().start();
    }

    /**
     * 自定义的post请求----------------------volley的简单的二次回调封装
     * @param mContext
     * @param url
     * @param tag
     * @param params
     * @param vif
     */
    public static void RequestPost(Context mContext,String url,String tag,
                  final Map<String,String> params,VolleyInterface vif){
        MyApplication.getHttpQueues().cancelAll(tag);
        stringRequest = new StringRequest(url,vif.loadingListener(),vif.errorListener()){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };
        stringRequest.setTag(tag);
        MyApplication.getHttpQueues().add(stringRequest);
        MyApplication.getHttpQueues().start();
    }

}
